package io.gitee.crud.util;

import org.springframework.util.StringUtils;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;

public class DateUtils {

    private static final String yyyyMMdd = "yyyy-MM-dd";
    private static final String yyyyMMddHHmmss = "yyyy-MM-dd HH:mm:ss";

    public static String parseDateTime(LocalDateTime dateTime) {
        if (dateTime == null) {
            return null;
        }
        return dateTime.format(DateTimeFormatter.ofPattern(yyyyMMddHHmmss));
    }

    public static LocalDateTime start(LocalDate date) {
        if (date == null) {
            return null;
        }
        return LocalDateTime.of(date, LocalTime.of(0,0,0));
    }

    public static LocalDateTime end(LocalDate date) {
        if (date == null) {
            return null;
        }
        return LocalDateTime.of(date, LocalTime.of(23,59,59));
    }

    public static LocalDate parseDate(String str) {
        if (!StringUtils.hasText(str)) {
            return null;
        }
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern(yyyyMMdd);
        return LocalDate.parse(str, formatter);
    }

    public static LocalDateTime parseDateTime(String str) {
        if (!StringUtils.hasText(str)) {
            return null;
        }
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern(yyyyMMddHHmmss);
        return LocalDateTime.parse(str, formatter);
    }



}
